<?php

/**
 * Base class for search paramter.any sort of param class (stored/dynamic)
 * should extend this.
 *
 * @abstract
 * @author Ishtiaque Shahrier <md.ishtiaque@newgenbd.net>
 * @package search
 * @subpackage libraries
 */
abstract class a_base_param_l_search implements i_param_l_search {

    /**
     * @var int Unique Id of the param
     */
    protected $id;
    /**
     * @var string describe the type of the param
     *
     * @see module_u_search::SEARCH_TYPE_PARAM
     */
    protected $type;
    /**
     * @var string value that you want to set and use later in your query
     */
    protected $option;
    /**
     * @var string label/text for the param. Generally you want this for templating
     */
    protected $label;

    /**
     *
     * @param int $id
     * @param string $type
     * @param string $option
     * @param string $label
     */
    public function __construct($id, $type, $option=NULL, $label=NULL) {
        $this->id = $id;
        $this->type = $type;
        $this->label = $label;
        $this->option = $option;
    }

    /**
     * Before returning option it does onvoke the a_base_param_l_search::translate($string)
     * function for getting real value.
     * @see a_base_param_l_search::translate
     *
     * @return string
     */
    public function get_option() {        
        return $this->translate($this->option);
    }

    /**
     *
     * @return int
     */
    public function get_id() {
        return $this->id;
    }

    /**
     *
     * @return string
     */
    public function get_type() {
        return $this->type;
    }

    /**
     *
     * @return string
     */
    public function get_label() {
        return $this->label;
    }

    /**
     * A utility function to check if the peiece of string
     * is a valid ORDER clause option
     * 
     * @param string $string
     * @return bool
     */
    protected function is_valid_order_direction($string) {
        if ($string != NULL) {
            $option_tmp = strtoupper($string);
            if ($option_tmp === "ASC" || $option_tmp === "DESC") {
                return TRUE;
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }
    /**
     * This function can detect the meaning of a string
     * and returns the value.This is used in conjunction
     * with the get_option() function to get the real value.
     * For example "post:user_id" optional value actually meant the
     * $_POST['user_id'] value
     *
     * @param  $string
     * @return string the translater/non-translated value
     */
    protected function translate($string) {
        $parts = explode(":", $string);
        $return = NULL;
        if (count($parts)) {
            switch ($parts[0]) {
                case "post":
                    $return = get_instance()->input->post($parts[1]);
                    break;
                case "url":
                    $arr = get_instance()->uri->uri_to_assoc();
                    $return = @$arr[$parts[1]];
                    break;
                case "func":
                    $return = $this->$parts[1]();
                    break;
                default:
                    $return = $string;
                    break;
            }

            return $return;
        } else {
            return $string;
        }
    }

    /**
     * @internal for debugging. prints the object
     */
    public function to_string() {
        debug_dump($this);
    }
}

?>